<template>
	<view>
    <zh-sheet v-if="showBind" :margin="[20, 10]" :padding="[24, 20]" :shadow="2" :round="6">
		<view class="flex-row-center-between" >
			<view style="border-left: 10rpx solid #2f54eb;">
				<zh-text  :fontSize="36" class="text-weight-b pl-10" label="是否绑定摇号"></zh-text>
			</view >
			<zh-radio-group v-model="form.bindFlag" @change="bindChange">
				<zh-radio  value="1" label="是"></zh-radio>
				<zh-radio :followTheme="false" color="orange" value="0" label="否"></zh-radio>
			</zh-radio-group>
		</view>
		<zh-sheet v-if="form.bindFlag === '1'" :margin="[0, 20]" :round="3" :padding="[20, 20]" color="deep-orange" text>
            <zh-text class="text-weight-b" label="温馨提示:"></zh-text>
            <zh-text style="text-indent: 2em; padding-top: 10rpx" label="以下是已报名的孩子，请勾选需要绑定摇号的孩子"></zh-text>
        </zh-sheet>
        <zh-checkbox-group v-if="form.bindFlag === '1'" v-model="checkboxlist">
        	<zh-checkbox :key="'childbind'+index" v-for="(item, index) in bindList" :disabled="item.bindFlag==='1'" :value="item.idcard" :label="item.fullName"></zh-checkbox>
        </zh-checkbox-group>
	</zh-sheet>
	</view>
</template>
<script lang="ts" setup name="followInfo">
import * as util from '@/tool/function/util'
//长幼随学信息
import {watch,ref} from "vue";
	// ------------------接收传入参数(组件)-------------------
	const props = defineProps({
		modelValue: {
		  type: Object,
		  default: () => {
			return {
				
			}
		  }
		}
	})
	const emits = defineEmits([
	  "update:modelValue",
	]);
	//---------------内部变量--------------------
	const form=ref<any>(props.modelValue)
	const showBind = ref(false);
	const bindList = ref<any>([]);
	const bindListAll = ref<any>([]);
	const checkboxlist=ref<any>([]);
	//----------------内部方法--------------------
const bindChange=(value:String)=>{
	if(value==='0'){
        form.value.checkboxlist=[]
	}
}
const checkBind = () => {
	bindListAll.value =[]
	bindList.value=[]
	showBind.value = false;
	checkboxlist.value=[]
  if(!form.value.schoolId){
	return
  }
  if(!form.value.childIdcard){
	return
  }else{
	let checkBirthday=false;
	if (form.value.childIdcard.length===18&&util.isIdCard(form.value.childIdcard)) {
		checkBirthday=false;
	}else{
		checkBirthday=true;
	}
	if(checkBirthday){
		if(!form.value.birthYmd){
			return
		}
	}
  }
  if(!form.value.fatherIdcard&&!form.value.motherIdcard){
	return
  }
  if(!form.value.formId){
	return
  }
  let params = { schoolId: form.value.schoolId,idcard:form.value.childIdcard,birthYmd:form.value.birthYmd,fatherIdcard:form.value.fatherIdcard,motherIdcard:form.value.motherIdcard };
  $api.common.send
    .post('kindergarten0027', params)
    .then((res: any) => {
      //保存成功后提示
      if (res.result&&res.result.length > 0) {
        let list=res.result;
        bindListAll.value = res.result;
        let bindListFilter=list.filter((item: any) => {
			return item.configType ===form.value.configType&&item.formId ===form.value.formId;
		});
		if(bindListFilter&&bindListFilter.length>0){
			showBind.value = true;
			let idcardList=[] as any
			bindListFilter.forEach((item:any)=>{
				idcardList.push(item.idcard)
			})
			checkboxlist.value=idcardList
		}else{
			showBind.value = false;
			checkboxlist.value=[]
		}
		bindList.value=bindListFilter
      } else {
        showBind.value = false;
		checkboxlist.value=[]
      }

    })
    .catch(() => {
      showBind.value = false;
      checkboxlist.value=[]
    });
};
watch(
	() => props.modelValue,
	() => {
	    form.value=props.modelValue
	},
	{
	    deep:true
	}
	  
)
	
watch(
	() => form.value,
	() => {
		//父组件发送数据
		emits("update:modelValue", form.value);
	},
	{
		deep: true,
	}
);

watch(
  () => checkboxlist.value,
  () => {
    //父组件发送数据
    form.value.bindIdcardList= checkboxlist.value
    emits('update:modelValue', form.value);
  },
  {
    deep: true,
  }
);

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ checkBind });
</script>